#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
const int N = 5005;
inline int read() {
	int x = 0, ch = getchar();
	bool f = false;
	while (!isdigit(ch)) {
		if (ch == '-') f = true;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return f ? -x : x;
}
int n, m, s, dis[N];
bool vis[N][N];
int e_cnt, heads[N];
struct Edge {
	int v, nxt;
} e[N << 1];

std::priority_queue< std::pair< int, int > > pq;
inline void add(int u, int v) { e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt; }
int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
	freopen("path.in", "r", stdin);
	freopen("path.out", "w", stdout);
#endif
#endif
	n = read(), m = read(), s = read();
	int l1, r1, l2, r2;
	while (m--) {
		l1 = read(), r1 = read(), l2 = read(), r2 = read();
		for (int i = l1; i <= r1; ++i) {
			for (int j = l2; j <= r2; ++j) {
				if (vis[i][j]) continue;
				add(i, j);
				add(j, i);
				vis[i][j] = true;
			}
		}
	}
	memset(dis, 0x3f, sizeof(dis));
	dis[s] = 0;
	pq.push(std::make_pair(0, s));
	while (!pq.empty()) {
		int u = pq.top().second, d = -pq.top().first;
		pq.pop();
		if (d != dis[u]) continue;
		for (int i = heads[u]; i; i = e[i].nxt) {
			int v = e[i].v;
			if (dis[v] > dis[u] + 1) {
				dis[v] = dis[u] + 1;
				pq.push(std::make_pair(-dis[v], v));
			}
		}
	}
	for (int i = 1; i <= n; ++i) { printf("%d\n", dis[i]); }
	return 0;
}